import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AdminFrame extends JFrame {
    public AdminFrame() {
        setTitle("MBTI职业性格测试 - 管理员界面");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5, 1));

        JButton viewResultsButton = new JButton("查看用户测试结果");
        viewResultsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 弹出输入框让管理员输入账号
                String account = JOptionPane.showInputDialog(AdminFrame.this, "请输入要查看测试结果的用户账号:");
                if (account != null && !account.isEmpty()) {
                    ResultDAOImpl resultDAO = new ResultDAOImpl();
                    Result result = resultDAO.findByAccount(account);
                    if (result != null) {
                        // 显示该账号的测试结果
                        StringBuilder resultInfo = new StringBuilder();
                        resultInfo.append("账号: ").append(result.getAccount()).append(", MBTI类型: ").append(result.getMbti());
                        JOptionPane.showMessageDialog(AdminFrame.this, resultInfo.toString(), "用户测试结果", JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        // 未找到该账号的测试结果，给出提示
                        JOptionPane.showMessageDialog(AdminFrame.this, "未找到该账号的测试结果，请检查账号是否正确。", "提示", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }
        });
        panel.add(viewResultsButton);


        JButton modifyQuestionButton = new JButton("修改题目");
        modifyQuestionButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ModifyQuestionFrame().setVisible(true);
            }
        });
        panel.add(modifyQuestionButton);

        // 删除用户按钮
        JButton deleteUserButton = new JButton("删除用户");
        deleteUserButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String account = JOptionPane.showInputDialog(AdminFrame.this, "请输入要删除的用户账号:");
                if (account != null && !account.isEmpty()) {
                    UserDAO userDAO = new UserDAOImpl();
                    userDAO.deleteUser(account);
                    JOptionPane.showMessageDialog(AdminFrame.this, "用户删除成功！");
                }
            }
        });
        panel.add(deleteUserButton);

        // 重置用户密码按钮
        JButton resetPasswordButton = new JButton("重置用户密码");
        resetPasswordButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String account = JOptionPane.showInputDialog(AdminFrame.this, "请输入要重置密码的用户账号:");
                if (account != null && !account.isEmpty()) {
                    String newPassword = JOptionPane.showInputDialog(AdminFrame.this, "请输入新密码:");
                    if (newPassword != null && !newPassword.isEmpty()) {
                        UserDAO userDAO = new UserDAOImpl();
                        userDAO.resetUserPassword(account, newPassword);
                        JOptionPane.showMessageDialog(AdminFrame.this, "用户密码重置成功！");
                    }
                }
            }
        });
        panel.add(resetPasswordButton);



        JButton logoutButton = new JButton("注销");
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new AdminLoginFrame().setVisible(true);
            }
        });
        panel.add(logoutButton);

        add(panel);
    }
}